<template>
  <div class="articleShow">
    <el-card class="box-card" shadow="never">
      <div class="card-header">
        <span class="text-large">精选咨询师</span>
      </div>
      <div class="my-divider-black-middle"></div>

      <div class="content">
        <!-- 咨询师列表 -->
        <div class="counselor-list" v-loading="loading">
          <el-row :gutter="20">
            <el-col :span="6" v-for="counselor in counselorList" :key="counselor.user_id">
              <el-card 
                class="counselor-card" 
                :shadow="'hover'"
                :body-style="{ padding: '15px' }"
                @click="goDetails(counselor)"
              >
                <!-- 头像和基本信息 -->
                <div class="counselor-header">
                  <el-avatar :size="60" :src="'' || '/default-avatar.jpg'" />
                  <div class="counselor-basic">
                    <h3 class="name">{{ counselor.name }}</h3>
                  </div>
                </div>

                <!-- 资质标签 -->
                <div class="qualification">
                  <el-tag size="small" type="info" effect="plain">
                    {{ `国家${counselor.level}级咨询师` }}
                  </el-tag>
                </div>

                <!-- 专业领域 -->
                <div class="expertise">
                  <p>擅长：{{ counselor.tag }}</p>
                </div>

                <!-- 预约按钮 -->
                <el-button type="primary" class="reserve-btn" @click.stop="goDetails(counselor)">
                  预约
                </el-button>
              </el-card>
            </el-col>
          </el-row>
        </div>

        <div class="my-divider-grey-middle"></div>

        <div class="btn">
          <el-button class="button-more text-middle" text @click="loadMore">
            更多咨询师
            <DArrowRight style="width: 1em; height: 1em; margin-right: 8px" />
          </el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { DArrowRight } from '@element-plus/icons-vue';
import router from '@/router';
import { navBarSelectState } from '@/stores/info/index';
import CounselorApi from '@/api/counselor';
import type { UserCounselorData } from '@/api/counselor/types';
import { ElMessage } from 'element-plus';

const selectedBar = navBarSelectState();
const loading = ref(false);
const counselorList = ref<UserCounselorData[]>([]);

// 获取推荐咨询师列表
const getRecommendCounselors = async () => {
  try {
    loading.value = true;
    const response = await CounselorApi.getUserCounselors({
      is_recommend: 1,
      is_delete: 0,
      Size: 4
    });
    
    if (Array.isArray(response.data)) {
      counselorList.value = response.data;
    }
  } catch (error) {
    ElMessage.error('获取咨询师列表失败');
    console.error('获取咨询师列表失败:', error);
  } finally {
    loading.value = false;
  }
};

// 跳转到详情页
const goDetails = (counselor: UserCounselorData) => {
  router.push({
    path: '/reservePage/details',
    query: { 
      user_id: counselor.user_id,
      fromWhere: 'home'
    }
  });
};

// 加载更多
const loadMore = () => {
  selectedBar.setSelected(1);
  router.push({
    name: 'reservePage',
  });
};

onMounted(() => {
  getRecommendCounselors();
});
</script>

<style scoped>
.content {
  padding: 20px 20px 0 20px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  height: 400px; 
}


.box-card {
  width: 100%; 
  height: 540px;
}

.counselor-list {
  flex: 1; /* 占据剩余空间 */
  overflow: hidden; /* 防止内容溢出 */
  margin-top: 20px;
}

.counselor-card {
  padding: 15px;
  border-radius: 12px;
  height: 280px;
  gap: 8px;
  text-align: center;
  margin-bottom: 0;
  border: 2px solid #e4e7ed;
}

.counselor-header {
  display: flex;
  flex-direction: column; /* 改为纵向排列 */
  align-items: center; /* 水平居中 */
  gap: 8px;
}

.counselor-card:hover {
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12), 
              0 3px 6px rgba(0, 0, 0, 0.08);
  transform: translateY(-2px);
  border-color: #c6e2ff; /* 悬浮时的边框颜色 */
}

.counselor-basic {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.name {
  margin: 0;
  font-size: 16px;
  font-weight: 500;
}

.help-count {
  margin: 4px 0 0;
  font-size: 12px;
  color: #666;
}

.qualification {
  margin-top: 20px;
}

.expertise {
  font-size: 14px;
  color: #666;
  margin: 20px 0;
}

.expertise p{
  margin: 0;
  line-height: 1.5;
}

.reserve-btn {
  width: 100%;
  margin-top: auto;
}

.button-more {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto;
}
</style>
